package com.rfchina.okr.xianren.util;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.jfinal.ext.kit.DateKit;
import com.jfinal.kit.StrKit;

public class SqlUtil {

	public static String like(String sqlExceptSelect, String value, String title, List<Object> where) {

		if (StrKit.notBlank(value)) {
			sqlExceptSelect += " and " + title + " like ? ";
			where.add("%" + value + "%");
		}

		return sqlExceptSelect;
	}

	public static String equalsIgnoreCase(String sqlExceptSelect, String value, String title, List<Object> where) {
		if (StrKit.notBlank(value)) {
			sqlExceptSelect += " and " + title + " = ? ";
			where.add(value);
		}
		return sqlExceptSelect;
	}

	public static String equals(String sqlExceptSelect, Integer value, String title, List<Object> where) {
		if (value != null) {
			sqlExceptSelect += " and " + title + " = ? ";
			where.add(value);
		}
		return sqlExceptSelect;
	}

	public static String equals(String sqlExceptSelect, Long value, String title, List<Object> where) {
		if (value != null) {
			sqlExceptSelect += " and " + title + " = ? ";
			where.add(value);
		}
		return sqlExceptSelect;
	}

	public static String in(String sqlExceptSelect, Object[] value, String title, List<Object> where) {
		if (value == null || value.length == 0) {
			return sqlExceptSelect;
		}
		sqlExceptSelect += " and  " + title + " in (";
		for (Object object : value) {
			sqlExceptSelect += "?,";
			where.add(object);
		}
		if (sqlExceptSelect.endsWith(",")) {
			sqlExceptSelect = sqlExceptSelect.substring(0, sqlExceptSelect.length() - 1);
		}
		sqlExceptSelect += ") ";
		return sqlExceptSelect;
	}

	public static String dateBetweenByDay(String sqlExceptSelect, Date value, String title, List<Object> where) {
		if (value != null) {
			sqlExceptSelect += " and " + title + " >=? and  " + title + " <? ";
			Calendar cal = Calendar.getInstance();
			cal.setTime(value);
			cal.set(Calendar.MILLISECOND, 0);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.HOUR_OF_DAY, 0);
			where.add(cal.getTime());
			Calendar cal1 = Calendar.getInstance();
			cal1.setTime(cal.getTime());
			cal1.add(Calendar.DATE, 1);
			where.add(cal1.getTime());
		}
		return sqlExceptSelect;
	}

	public static String date(String sqlExceptSelect, Date value1, Date value2, String title, List<Object> where) {
		if (value1 != null) {
			sqlExceptSelect += " and " + title + " >=? ";
			where.add(value1);
		}
		if (value2 != null) {
			sqlExceptSelect += " and " + title + " <=? ";
			where.add(value2);
		}

		return sqlExceptSelect;
	}

	public static String integer(String sqlExceptSelect, Integer value, String title, List<Object> where) {
		if (value != null && value > 0) {
			sqlExceptSelect += " and " + title + " = ? ";
			where.add(value);
		}
		return sqlExceptSelect;
	}

	public static String longValue(String sqlExceptSelect, Long value, String title, List<Object> where) {
		if (value != null && value >= 0) {
			sqlExceptSelect += " and " + title + " = ? ";
			where.add(value);
		}
		return sqlExceptSelect;
	}
}
